<?php

namespace app\controller;

use app\BaseController;
use app\model\SysClass;
use app\utils\CamlHump;
use app\utils\ResposeUtils;
use think\facade\Log;
use think\facade\Request;

class SysClassController  extends BaseController
{


    public function list()
    {
        $body = Request::post();
        $query = SysClass::createQuery($body);
        //注入haveNext 
        $list= CamlHump::toCamlArray($query->select());



        
        for($i=0;$i<count($list);$i++){
            $parentId=$list[$i]['id'];
            if(isset($parentId)&&$parentId>0){
                $list[$i]['haveNext']=(SysClass::createQueryByParentId($parentId)->count())>0;
            }
        }
        return ResposeUtils::ok(
            $list ,
            200,
            $query->count()
        );
    }


    function getbyid()
    {
        
        $id = Request::get("id");
        $one = SysClass::findOne($id);




        //查询父类的名称
        if(isset( $one['parentId'])&&$one['parentId']>0){
            $parentObj= SysClass::findOne($one['parentId']);
            Log::info("获取到的父类信息是");
            $one ['parentName']=$parentObj['name']??'';
        }



        
        return ResposeUtils::ok(
            $one 
        );
    }
    function update()
    {
        $body = Request::post();
        $updateersuilt=SysClass::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function add()
    {
        $body = Request::post();
        $result=SysClass::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function delbyid()
    {
        $id = Request::get('id');
        $result=SysClass::destroy($id);
        return ResposeUtils::ok(
            $result
        );
    }
}
